import { Component } from '@angular/core';
import { SelectItem } from 'primeng/api';

import { EmailList } from '../../model/email-list';
import { EmailInfoService } from 'src/app/service/email-info.service';

@Component({
  selector: 'app-email',
  templateUrl: './email.component.html',
  styleUrls: ['./email.component.css']
})
export class EmailComponent {
  emailSubject: string;
  addressList: SelectItem[];
  selectedAddress: string[] = [];
  emailContent: string;
  emailResult: string;
  displaySuccess = false;

  constructor(private emailService: EmailInfoService) {
    this.addressList = EmailList;
  }

  submitEamilInfo() {
    const emailInfo = {
      toAddress: this.selectedAddress,
      subject: this.emailSubject,
      content: this.emailContent,
    };

    this.emailService.sendEmailInfo(JSON.stringify(emailInfo)).subscribe(
      res => {
        if (res === 'ok') {
          this.emailResult = '邮件发送成功！！！厉害了我的哥。';
        } else {
          this.emailResult = '没有成功...再接再厉！！！';
        }
        this.displaySuccess = true;
      },
      error => {
        this.emailResult = '没有成功...再接再厉！！！';
        this.displaySuccess = true;
      }
    );

  }

  closeSuccess() {
    this.displaySuccess = false;
  }
}
